package com.simon.hse.activity.safe.safework;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.activity.safe.jsa.JsaAnalysisAdministrationListActivity;
import com.simon.hse.activity.safe.jsa.JsaDetailsActivity;
import com.simon.hse.activity.safe.workpermit.WorkPermitApplyActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.LicenseGetListInput;
import com.simon.hse.http.entity.LicenseGetListOutput;
import com.simon.hse.http.entity.SafetySchemeMobileGetListInput;
import com.simon.hse.http.entity.SafetySchemeMobileGetListOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 作业安全方案查询
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_work_permit_query)
public class SafeWorkQueryActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.iv_common_right)
    ImageView mIv_common_right;

    @ViewInject(R.id.tv_function_1)
    TextView mTv_function_1;
    @ViewInject(R.id.ll_function_1)
    LinearLayout mLl_function_1;

    @ViewInject(R.id.tv_function_2)
    TextView mTv_function_2;
    @ViewInject(R.id.ll_function_2)
    LinearLayout mLl_function_2;

    @ViewInject(R.id.tv_function_3)
    TextView mTv_function_3;
    @ViewInject(R.id.ll_function_3)
    LinearLayout mLl_function_3;

    @ViewInject(R.id.tv_function_4)
    TextView mTv_function_4;
    @ViewInject(R.id.ll_function_4)
    LinearLayout mLl_function_4;

    @ViewInject(R.id.common_swiperefreshlayout)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<SafetySchemeMobileGetListOutput.DataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTv_common_title.setText("作业安全方案查询");
        mIv_common_right.setImageResource(R.mipmap.tab_add_icon);
        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.iv_common_right, R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3, R.id.ll_function_4})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.iv_common_right:
                Intent intent = new Intent(SafeWorkQueryActivity.this, WorkSafePlanActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_1:
                initSafePop(mLl_function_1, mTv_function_1, MyApplication.getInstance().mSafeQureyStateList);
                break;
            case R.id.ll_function_2:
                initSafePop(mLl_function_2, mTv_function_2, MyApplication.getInstance().mSafePersonInChargeList);
                break;
            case R.id.ll_function_3:
                publishSafeDate(mTv_function_3);
                break;
            case R.id.ll_function_4:
                publishSafeDate(mTv_function_4);
                break;
        }
    }

    private void initRecyclerView() {
        mTv_function_1.setHint("状态");
        mTv_function_2.setHint("作业负责人");
        mTv_function_3.setHint("申请时间");
        mTv_function_4.setHint("有效时间");
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });
        LinearLayoutManager llm = new LinearLayoutManager(SafeWorkQueryActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);
        mCommonEmptyAdapter = new CommonEmptyAdapter(SafeWorkQueryActivity.this, R.layout.layout_item_safe_work_query, mDataBeanList);
        mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<SafetySchemeMobileGetListOutput.DataBean>() {
            @Override
            public void setItemDatas(CommonViewHolder holder, SafetySchemeMobileGetListOutput.DataBean dataBean, int position) {
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                Intent intent = new Intent(SafeWorkQueryActivity.this, WorkSafePlanDetailsctivity.class);
                intent.putExtra("from", "effective");
                startActivity(intent);
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });
        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        SafetySchemeMobileGetListInput safetySchemeMobileGetListInput = new SafetySchemeMobileGetListInput();
        SafetySchemeMobileGetListInput.ModelBean modelBean = new SafetySchemeMobileGetListInput.ModelBean();
        safetySchemeMobileGetListInput.setModel(modelBean);
        safetySchemeMobileGetListInput.setLimit(10);
        safetySchemeMobileGetListInput.setOffset(10 * (currentPage - 1));
        Call<String> call = HttpManager.getInstance().requestService().SafetySchemeMobile_GetList(HttpManager.getInstance().requestBody(safetySchemeMobileGetListInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                SafetySchemeMobileGetListOutput safetySchemeMobileGetListOutput = new Gson().fromJson(result, SafetySchemeMobileGetListOutput.class);
                if (safetySchemeMobileGetListOutput.getResult().equals("Success")) {
//                    totalPage = safetySchemeMobileGetListOutput.getData().getRowCount();
                    List<SafetySchemeMobileGetListOutput.DataBean> pageDataBeanList = safetySchemeMobileGetListOutput.getData();
                    mDataBeanList.clear();
                    mDataBeanList.addAll(pageDataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();
                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(safetySchemeMobileGetListOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
